<template>
  <view class="person">
    <view class="info" v-if="isLogin">
      <image class="avatar login_avatar" :src="detail.avatar"/>
      <view class="username">{{ detail.username }}</view>
    </view>
    <view class="info" v-else>
      <image class="avatar" src="../../assets/tabbar/user_active.png"/>
      <view class="loginBtn" @tap="login">立即登录</view>
    </view>
<!--    <view class="main">-->
<!--      <view class="main_item">-->
<!--        <image src="../../assets/icon/star.png" alt="" />-->
<!--        <text>我的收藏</text>-->
<!--      </view>-->
<!--      <view class="main_item">-->
<!--        <image src="../../assets/tabbar/author_active.png" alt="" />-->
<!--        <text>成为创作者</text>-->
<!--      </view>-->
<!--    </view>-->
    <view class="list">
      <view class="list_item" @tap="navigateTo('/pages/comment/index?type=1')">
        <view class="left">
          <image src="../../assets/tabbar/author_active.png" alt="" />
          <text>成为创作者</text>
        </view>
        <image src="../../assets/icon/right.png" alt="" />
      </view>
      <view class="list_item" @tap="navigateTo('/pages/content/index?id=3')">
        <view class="left">
          <image src="../../assets/icon/question.png" alt="" />
          <text>常见问题</text>
        </view>
        <image src="../../assets/icon/right.png" alt="" />
      </view>
      <view class="list_item" @tap="navigateTo('/pages/comment/index?type=2')" >
        <view class="left">
          <image src="../../assets/icon/comment.png" alt="" />
          <text>留言</text>
        </view>
        <image src="../../assets/icon/right.png" alt="" />
      </view>
      <view class="list_item" @tap="navigateTo('/pages/content/index?id=1')" >
        <view class="left">
          <image src="../../assets/icon/company.png" alt="" />
          <text>关于我们</text>
        </view>
        <image src="../../assets/icon/right.png" alt="" />
      </view>
      <view class="list_item" @tap="navigateTo('/pages/content/index?id=2')">
        <view class="left">
          <image src="../../assets/icon/version.png" alt="" />
          <text>版本信息</text>
        </view>
        <view class="right">
          <text>v2.0.1</text>
          <image src="../../assets/icon/right.png" alt="" />
        </view>
      </view>
      <view class="list_item" @tap="navigateTo('/pages/setting/index')">
        <view class="left">
          <image src="../../assets/icon/setting.png" alt="" />
          <text>设置</text>
        </view>
        <view class="right">
          <image src="../../assets/icon/right.png" alt="" />
        </view>
      </view>
      <button open-type="contact" class="contact">
        <image src="../../assets/icon/customer2.png" alt="" />
        联系客服
      </button>
    </view>
  </view>
</template>

<script>
  import './index.sass'
  import Taro from '@tarojs/taro'
  import {get, post} from "../../util/request";

  export default {
    data() {
      return {
        anonymousCode: '',
        code: '',
        detail: {
        },
        isLogin: false,
      }
    },
    methods: {
      navigateTo(url) {
        Taro.navigateTo({
          url: url
        })
      },
      async login() {
        var _this = this
        const rsp = await Taro.getUserProfile({
          async success(rsp) {
            const result = await post('login', {
              anonymousCode: _this.anonymousCode,
              code: _this.code,
              avatar: rsp.userInfo.avatarUrl,
              name: rsp.userInfo.nickName,
            })
            Taro.setStorageSync("token", result.token)
            _this.detail = result.customer
            _this.isLogin = true
          }
        })
      }
    },
    async onShow() {
      if (!Taro.getStorageSync("token")) {

        const res = await Taro.login()
        this.code = res.code
        this.anonymousCode = res.anonymousCode

        this.isLogin = false
      }
    },
    async onLoad(options) {

      const res = await Taro.login()
      this.code = res.code
      this.anonymousCode = res.anonymousCode

      if (Taro.getStorageSync("token")) {
        try{
          this.detail = await get('info')
          this.isLogin = true
        }catch (e) {
          Taro.removeStorageSync("token")
        }
      }
    }
  }
</script>
